<?php
include('rootlogin.php');
//include('../../incsession.php');
session_start();
$level=3;
if($_POST['submit']=="submit"){
	$i=1;
	$number=$_POST['OQ'];
	$num=$_POST['num'];
	while($i < $num)
	{	
		if($_POST['Q'.$i]=='' && $_POST['answer'.$i]==''){
			
			
			$query = "DELETE FROM open_question 
							WHERE oqid=".$_POST['oqid'.$i]." AND topicid=".$_SESSION['topic']." AND level=".$level;
			//echo $query."<br>";
			$result = mysql_query($query,$user_con);
		}
		if($_POST['Q'.$i]!='' && $_POST['answer'.$i]!='' && $_POST['Q'.$i]!=' ' && $_POST['answer'.$i]!=' '){
			
			
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			$query = "UPDATE open_question SET question='".$Q."',
							answer='".$A."'  
							WHERE oqid=".$_POST['oqid'.$i]." AND topicid=".$_SESSION['topic']." AND level=".$level;
			//echo $query."<br>";
			$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	while($i < $number+$num)
	{	
		if($_POST['Q'.$i]!='' && $_POST['answer'.$i]!=''){
			
			
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			$query = "INSERT INTO open_question (question, answer, topicid, level) 
				VALUES ('".$Q."',
							'".$A."', 
							".$_SESSION['topic'].", ".$level.")";
			//echo $query."<br>";
			$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	header("location: page.php");	
}
if(!isset($_POST['OQ'])){
			$OQ=0;
			$flag=1;
}
else if($_POST['submit']=='add'){
			$OQ=$_POST['OQ'];
			$OQ++;
			$flag=0;
}

?>

<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../framestyle.css" type="text/css" media="screen">
<style type="text/css">

</style>
</head>


<body>

<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FOR DIFFICULT LEVEL: OPEN QUESTIONS</div>
<hr/>
<br><br>
<form method = POST action="open_question3.php" >
<?php
$i=1;


$query = "SELECT * FROM open_question WHERE topicid=".$_SESSION['topic']." AND level=".$level;
$result = mysql_query($query,$user_con);
while($row = mysql_fetch_array($result)){
	if($flag==1){
		$Q=$row['question'];
		$A=$row['answer'];
	}else{
		$Q=$_POST['Q'.$i];
		$A=$_POST['answer'.$i];
	}
	$Q = str_replace("'", "&#39;", $Q);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $A);
	$A = str_replace('"', '&quot;', $A);
	echo '<table>';
	echo '<tr><td align="right">Q'.$i.': <td><input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'">
			<tr><td align="right">A'.$i.': <td><input class="area long" type="text" name="answer'.$i.'" value="'.$A.'"></table>';	
	echo "<input type='hidden' name='oqid".$i."' value=".$row['oqid']."><br>";
	$i++;	
}
$num=$i;


while($i<$OQ+$num){
	$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
	$A = str_replace('"', '&quot;', $A);
	echo '<table>';
	echo '<tr><td align="right">Q'.$i.': <td><input class="area long" type="text" name="Q'.$i.'" value="'.$_POST['Q'.$i].'">
			<tr><td align="right">A'.$i.': <td><input class="area long" type="text" name="answer'.$i.'" value="'.$_POST['answer'.$i].'"></table>';	
	echo "<br>";
	$i++;
}
echo "<input type='hidden' name='num' value=".$num.">";
echo "<input type='hidden' name='OQ' value=".$OQ.">";

?>

<div class="btn_right"><input type="submit" name = "submit" value = "add"></div><br>
<div class="btn_right"><input type="submit" name = "submit" value = "submit"></div>
</form>
</div>
</div>
</body>

